//
//  MainViewController.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/6/23.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        
        //获取tabBar
        
//        let tb = MainTabBar()
//        
//        setValue(tb, forKey: "tabBar")
//        
//        //加号按钮添加点击事件
//        tb.compoesButton.addTarget(self, action: "compoesButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    }
    

    
    func compoesButtonClick(){
       let nav = UINavigationController(rootViewController: ComposeViewController())
        presentViewController(nav, animated: true, completion: nil)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        let w = tabBar.bounds.width / CGFloat(5)
        let rect = CGRectMake(0, 0, w, tabBar.bounds.height)
        //整个向右移动2倍的w
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
     
    }
    
    private func addChildViewControllers() {
//        //获取json文件
//        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
//        
//        let data = NSData(contentsOfFile: path)!
//        
//        do{
//            //解析json文件
//            let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
//            for dict in array as! [[String: String]]{
//            addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
//            
//            }
//        
//        }catch{
//        
//            print(error)
//            
//            // 如果从网络加载的 json 出错，直接用字符串加载
//            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
//            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
//            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
//            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
//
//        
//        }
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController(), title: "", imageName: nil)
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    

    private func addChildViewController(vc:UIViewController, title:String, imageName:String?) {
        
        
//        //获取info.plist 的信息
//        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
//        
//        //将字符串转成类
//        let cls:AnyClass = NSClassFromString(ns + "." + vcName)!
//        
//        
//        //实例化控制器
//        let vc = cls.new() as! UIViewController
//        
//        //设置
//        vc.title = title
//        
//        vc.tabBarItem.image = UIImage(named: imageName)
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
//        
//        let nav = UINavigationController(rootViewController: vc)
        vc.title = title
        
        if imageName != nil{
            vc.tabBarItem.image = UIImage(named: imageName!)
            vc.tabBarItem.selectedImage = UIImage(named: imageName! + "_highlighted")
        
        }
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    //撰写按钮
    private lazy var composeButton: UIButton = {
        //实例化按钮
        let btn = UIButton()
        
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到tabBar
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "compoesButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    
    }()
}
